﻿/*
==================================================================
    注意：此文件为Auto代码，不可手动修改【数据权限】
==================================================================
*/

namespace Services
{
    /// <summary>
    /// Auto数据权限
    /// </summary>
    public abstract class AutoRegulatorDataMapping : BaseMapping<RegulatorData, RegulatorDataModel, RegulatorDataFilterModel>
    {
        public AutoRegulatorDataMapping(ICurrentUser currentUser)
            : base(currentUser) { }

        #region 读数据
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entity">Entity对象</param>
        /// <returns></returns>
        public override RegulatorDataModel Entity2Model(RegulatorData entity)
        {
            RegulatorDataModel model = new RegulatorDataModel();

            model.RegulatorDataID = entity.RegulatorDataID;
            model.RegulatorID = entity.RegulatorID;
            model.ProvinceID = entity.ProvinceID;
            model.CityID = entity.CityID;
            model.AreaID = entity.AreaID;
            model.IsSystem = entity.IsSystem;
            model.ParentID = entity.ParentID;

            return model;
        }
        #endregion

        #region 写数据
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="model">ViewModel对象</param>
        /// <returns></returns>
        public override RegulatorData Model2Entity(RegulatorDataModel model)
        {
            RegulatorData entity = new RegulatorData();

            entity.RegulatorDataID = model.RegulatorDataID;
            entity.RegulatorID = model.RegulatorID;
            entity.ProvinceID = model.ProvinceID;
            entity.CityID = model.CityID;
            entity.AreaID = model.AreaID;
            entity.IsSystem = model.IsSystem;
            entity.ParentID = model.ParentID;

            return entity;
        }
        #endregion

        #region 搜索条件转换
        /// <summary>
        /// 搜索条件转换，从FilterModel对象转换成Where对象
        /// </summary>
        /// <param name="filterModel">FilterModel对象</param>
        /// <returns></returns>
        public override Expressionable<RegulatorData> FilterModel2Where(RegulatorDataFilterModel filterModel)
        {
            var where = base.FilterModel2Where(filterModel);

            //编号
            if (filterModel._RegulatorDataID.HasValue)
            {
                where = where.And(x => x.RegulatorDataID == filterModel._RegulatorDataID.Value);
            }
            if (filterModel._RegulatorDataIDs.IsNotEmpty() && filterModel._RegulatorDataIDs.Count > 0)
            {
                where = where.And(x => filterModel._RegulatorDataIDs.Contains(x.RegulatorDataID));
            }
            if (filterModel._NotRegulatorDataIDs.IsNotEmpty() && filterModel._NotRegulatorDataIDs.Count > 0)
            {
                where = where.And(x => !filterModel._NotRegulatorDataIDs.Contains(x.RegulatorDataID));
            }

            //监管部门
            if (filterModel._RegulatorID.HasValue)
            {
                where = where.And(x => x.RegulatorID == filterModel._RegulatorID.Value);
            }
            if (filterModel._RegulatorIDs.IsNotEmpty() && filterModel._RegulatorIDs.Count > 0)
            {
                where = where.And(x => filterModel._RegulatorIDs.Contains(x.RegulatorID));
            }
            if (filterModel._NotRegulatorIDs.IsNotEmpty() && filterModel._NotRegulatorIDs.Count > 0)
            {
                where = where.And(x => !filterModel._NotRegulatorIDs.Contains(x.RegulatorID));
            }

            //省
            if (filterModel._ProvinceID.HasValue)
            {
                where = where.And(x => x.ProvinceID == filterModel._ProvinceID.Value);
            }
            if (filterModel._ProvinceIDs.IsNotEmpty() && filterModel._ProvinceIDs.Count > 0)
            {
                where = where.And(x => filterModel._ProvinceIDs.Contains(x.ProvinceID));
            }
            if (filterModel._NotProvinceIDs.IsNotEmpty() && filterModel._NotProvinceIDs.Count > 0)
            {
                where = where.And(x => !filterModel._NotProvinceIDs.Contains(x.ProvinceID));
            }

            //市
            if (filterModel._CityID.HasValue)
            {
                where = where.And(x => x.CityID == filterModel._CityID.Value);
            }
            if (filterModel._CityIDs.IsNotEmpty() && filterModel._CityIDs.Count > 0)
            {
                where = where.And(x => x.CityID.HasValue && filterModel._CityIDs.Contains(x.CityID.Value));
            }
            if (filterModel._NotCityIDs.IsNotEmpty() && filterModel._NotCityIDs.Count > 0)
            {
                where = where.And(x => x.CityID.HasValue && !filterModel._NotCityIDs.Contains(x.CityID.Value));
            }

            //县
            if (filterModel._AreaID.HasValue)
            {
                where = where.And(x => x.AreaID == filterModel._AreaID.Value);
            }
            if (filterModel._AreaIDs.IsNotEmpty() && filterModel._AreaIDs.Count > 0)
            {
                where = where.And(x => x.AreaID.HasValue && filterModel._AreaIDs.Contains(x.AreaID.Value));
            }
            if (filterModel._NotAreaIDs.IsNotEmpty() && filterModel._NotAreaIDs.Count > 0)
            {
                where = where.And(x => x.AreaID.HasValue && !filterModel._NotAreaIDs.Contains(x.AreaID.Value));
            }

            //是否为系统建立
            if (filterModel._IsSystem.HasValue)
            {
                where = where.And(x => x.IsSystem == filterModel._IsSystem.Value);
            }
            if (filterModel._IsSystems.IsNotEmpty() && filterModel._IsSystems.Count > 0)
            {
                where = where.And(x => filterModel._IsSystems.Contains(x.IsSystem));
            }
            
            return where;
        }
        #endregion
    }
}

